//
//  LoginViewModel.m
//  BrushFace
//
//  Created by Zzz on 15/11/29.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "LoginViewModel.h"
#import "LoginRequest.h"
#import "WeChat.h"
#import "MeStatus.h"
#import "AlertDialogHelper.h"
#import <CloudPushSDK/CloudPushSDK.h>
#import <ALBBSDK/ALBBSDK.h>
@implementation LoginViewModel
-(void)loadSceneModel{
    [super loadSceneModel];
}

- (void)login:(LonginCb)cb
{
    @weakify(self);
    NSString * loginKey = [[MeStatus sharedInstance]getLoginKey];
    USER_ACCOUNT_STATUS status = [MeStatus sharedInstance].userAccountStatus;
    if (status == USER_ACCOUNT_STATUS_VERIFY && loginKey.isNotEmpty)
    { // 登录时已经有了loginkey , 获取临时token, 根据临时token进行登录, 最后获得key,直接保存
        [self sendGetTmpTokenWithKey:@"123" cb:^(id recvData, TYPE_LONGIN type) {
            @strongify(self);
            if (type == TYPE_LONGIN_TMP_TOKEN_FAILE) {
                cb(recvData, TYPE_LONGIN_TMP_TOKEN_FAILE);
                return ;
            }
            // 成功获取到了临时的token
            NSString * tmpToken = recvData;
            [self sendLoginByKey:loginKey tmpToken:tmpToken cb:cb];
        }];
        
        return;
    }
    // 还没有登录成功过,微信授权登录
    if (![WXApi isWXAppInstalled]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未安装微信，请安装后重新尝试" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return ;
    }
    [[WeChat sharedInstance]sendAuthRequest:^(id result)
    {
        @strongify(self);
        if (!result)
        {
            ITLog(@"获取权限失败");
            return ;
        }
        [self sendLoginByCode:result cb:cb];
    }];
}

- (void)sendLoginByCode:(NSString *)code cb:(LonginCb)cb
{
    LoginRequest * loginByCodeRq = [LoginRequest createLoginByCodeRq];
    loginByCodeRq.code = [WeChat sharedInstance].loginResp.code;
    [self sendRequest:loginByCodeRq success:^{
        NSString * loginTmpKey = [loginByCodeRq.output objectAtPath:kRecvLoginKey];
        ITLogSP(@"%@",[loginByCodeRq.output objectForKey:@"outid"]);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loginsuccess" object:nil];
        // 直接保存登录key
        [CloudPushSDK unbindAccount:[loginByCodeRq.output objectForKey:@"outid"] withCallback:^(BOOL success) {
            
            
            
                [CloudPushSDK bindAccount:[loginByCodeRq.output objectForKey:@"outid"] withCallback:^(BOOL success) {
                    if (success) {
                        ITLog(@"绑定成功");
                    }
                }];
           
        }];
        cb(loginTmpKey,TYPE_LONGIN_KEY_SUCCESS);
    } error:^{
        NSString * errorMsg = [loginByCodeRq.output objectAtPath:kRecvErrorMsg];
        ITLogSP(@"%@",errorMsg);
        cb(errorMsg,TYPE_LONGIN_KEY_FAILE);
    }];
}

- (void)sendGetTmpTokenWithKey:(NSString *)key cb:(LonginCb)cb
{
    LoginRequest * rq = [LoginRequest createTokenForAppLoginRq];
    rq.key = key;
    [self sendRequest:rq success:^{
        NSString * longinTmpToken = [rq.output objectAtPath:kRecvLoginToken];
        ITLogSP(@"%@",longinTmpToken);
        cb(longinTmpToken,TYPE_LONGIN_TMP_TOKEN_SUCCESS);
    } error:^{
        NSString * errorMsg = [rq.output objectAtPath:kRecvErrorMsg];
        ITLogSP(@"%@",errorMsg);
        cb(errorMsg,TYPE_LONGIN_TMP_TOKEN_FAILE);
    }];
}

- (void)sendLoginByKey:(NSString *)key tmpToken:(NSString *)tmpToken cb:(LonginCb)cb
{
    LoginRequest * rq = [LoginRequest createLoginByLoginKeyRq];
    rq.loginKey = key;
    rq.loginToken = tmpToken;
    [self sendRequest:rq success:^{
        NSString * longinKey = [rq.output objectAtPath:kRecvLoginKey];
        
        [[MeStatus sharedInstance]saveLoginKey:longinKey];
        cb(longinKey,TYPE_LONGIN_TMP_TOKEN_SUCCESS);
    } error:^{
        NSString * errorMsg = [rq.output objectAtPath:kRecvErrorMsg];
        ITLogSP(@"%@",errorMsg);
        cb(errorMsg,TYPE_LONGIN_TMP_TOKEN_FAILE);
    }];
}
@end
